Option Public

Function CheckFieldValues ( DocRef As NotesDocument )
	
	Dim MsgText As String
	CheckFieldValues = True
	MsgText = ""
	If DocRef.Topic(0) = "" Then
		MsgText = MsgText + "Specify a topic description." + Chr$(13)
	End If		
	
	If DocRef.Category(0) = "" Then
		MsgText = MsgText + "Specify a discussion category" + Chr$(13)
	End If	
	
	If MsgText <> "" Then
		Msgbox MsgText, 16, "Required Fields."
		CheckFieldValues = False
	End If	
	
End Function

Sub SendNotice ( uidoc As NotesUIDocument ) 
	
	'---------------------------------------------------------------------------------------
  	' Initialize primary objects
	'---------------------------------------------------------------------------------------	
	Dim s As New NotesSession
	Dim db As NotesDatabase
	Dim ws As New NotesUIWorkspace
	Dim doc As NotesDocument
	Dim viewA As NotesView
	Dim docA As NotesDocument
	Dim rtitem As NotesRichTextItem
	Set s = New NotesSession	
	Set db = s.CurrentDatabase
	Set doc = uidoc.Document
	Set viewA = db.GetView( "Subscriptions" )
	Set docA = viewA.GetFirstDocument
	
	'---------------------------------------------------------------------------------------
  	' Build the email document
	'---------------------------------------------------------------------------------------	
	Dim docMemo As NotesDocument
	Set docMemo = New NotesDocument (db)
	docMemo.Form = "Memo"
	docMemo.Subject = doc.Topic
	Set rtitem = New NotesRichTextItem(docMemo, "Body") 
	Call rtitem.AddNewLine( 1 )
	Dim rtitemB As Variant
	Set rtitemB = doc.GetFirstItem( "Body" )
	Call rtitem.AppendRTItem( rtitemB )
	Call rtitem.AddNewLine( 2 )	
	Call rtitem.AppendText ("Open Document --> " )
	Call rtitem.AppendDocLink( Doc, "Discussion Topic" )  
	Call rtitem.AddNewLine( 1 )	
	Call rtitem.AppendText ("Open Database --> " )
	Call rtitem.AppendDocLink( db, db.Title )  
	
	'---------------------------------------------------------------------------------------
     ' Loop through subscriptions.  Send email to interested parties. 
	'---------------------------------------------------------------------------------------
	While Not ( docA Is Nothing )
		'Msgbox "Sending email to: " + docA.InternetMail(0)
		If docA.AllowEmails(0) = "-1" Then		
			If ( foundElement (docA.Category, doc.Category(0) )) Then			
				Print "--- Sending email to: " + docA.InternetMail(0)	
				docMemo.SendTo = docA.InternetMail(0)
				docMemo.Send False						
			End If
		End If	
		Set docA = viewA.GetNextDocument( docA )
	Wend	
	
End Sub

Sub SendNewPostings
	
	Dim s As NotesSession
	Dim db As NotesDatabase
	Dim viewA As NotesView
	Dim viewB As NotesView	
	Dim docA As NotesDocument
	Dim docB As NotesDocument
	Dim docC As NotesDocument
	Dim docMemo As NotesDocument	
	Dim rtitem As NotesRichTextItem
	Dim rtitemB As Variant	
	Set s = New NotesSession
	Set db = s.CurrentDatabase	
	Set viewA = db.GetView( "EmailQue" )
	Set docA = viewA.GetFirstDocument
	Set viewB = db.GetView( "Subscriptions" )
	
	'--------------------------------------------------------------------------
     ' Loop through email queue.  
	'--------------------------------------------------------------------------	
	While Not ( docA Is Nothing )
		Print "Processing topic: " + docA.Topic(0)
		
		'-----------------------------------------------------------------------
  		' Build the email document
		'-----------------------------------------------------------------------
		Set docMemo = New NotesDocument (db)
		docMemo.Form = "Memo"
		docMemo.Subject = docA.Topic
		Set rtitem = New NotesRichTextItem(docMemo, "Body") 
		Call rtitem.AddNewLine( 1 )
		Set rtitemB = docA.GetFirstItem( "Body" )
		Call rtitem.AppendRTItem( rtitemB )
		Call rtitem.AddNewLine( 2 )	
		Call rtitem.AppendText ("Open Document --> " )
		Call rtitem.AppendDocLink( DocA, "Discussion Topic" )  
		Call rtitem.AddNewLine( 1 )	
		Call rtitem.AppendText ("Open Database --> " )
		Call rtitem.AppendDocLink( db, db.Title )  
		
		'--------------------------------------------------------------------------
    	 	' Send email to interested parties
		'--------------------------------------------------------------------------		
		Set docB = viewB.GetFirstDocument
		While Not ( docB Is Nothing ) 		
			If docB.AllowEmails(0) = True Then		
				If ( foundElement (docB.Category, docA.Category(0) )) Then			
					Print "--- Sending email to: " + docB.InternetMail(0)	
					docMemo.SendTo = docB.InternetMail(0)
					docMemo.Send False						
				End If
			End If
			Set docB = viewB.GetNextDocument ( docB ) 	
		Wend
		
		'--------------------------------------------------------------------------
    	 	' Get next document before updating current status.
		'--------------------------------------------------------------------------		
		Set docC = viewA.GetNextDocument( docA )		
		docA.Status = "COMPLETE"
		docA.Save True, True
		Set docA = docC
	Wend	
	
End Sub

Function foundElement (varFromList As Variant, varTarget As Variant) As Variant
	
	'-----------------------------------------------------------------------
	' Check for the existance of an element in an array.
	'-----------------------------------------------------------------------	
	foundElement = False
	Forall varElement In varFromList
		If varElement = "All" Then 
			foundElement = True
			Exit Forall
		Elseif varElement = varTarget Then
			foundElement = True
			Exit Forall		
		End If
	End Forall
	
End Function

Function SaveForm As Integer
	
	Dim ws As New NotesUIWorkspace
	Dim uidoc As NotesUIDocument 
	Dim doc As NotesDocument
	Dim answer As Integer
	Dim Continue As Integer
	Set uidoc = ws.CurrentDocument
	Set doc = uidoc.Document
	
	Continue = CheckFieldValues ( doc )
	If Continue Then	
		If doc.IsNewNote Then
			answer% = Msgbox("Are you ready to post this topic?", 292, "Continue?")
			If answer% = 6 Then
				doc.Status = "SUBMIT"
				uidoc.Close			
			Else
				continue = False
			End If
		End If
	End If
	SaveForm = Continue
	
End Function

Sub CloseForm 
	
	Dim w As New NotesUIWorkspace
	Dim uidoc As NotesUIDocument
	Dim doc As NotesDocument
	Set uidoc = w.CurrentDocument
	Set doc = uidoc.Document
	If doc.Status(0) = "SUBMIT" Then	
		
		' Comment out the "SendNotice" statement to
		' implement the server-based email notifications
		Call SendNotice ( uidoc )
		
		doc.Status = "POSTED"
		doc.Save True, True
	End If	
	
End Sub
